import json
import os

import logging

import requests

# 配置日志记录以捕获调度器的错误
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)



def select_user(user, device_id):

    data = {
        'device_id': device_id,
        'user': user,
    }

    # 获取当前脚本的文件路径
    current_file_path = os.path.abspath(__file__)

    # 获取当前脚本所在的文件夹路径
    current_directory = os.path.dirname(current_file_path)

    # 读取 JSON 文件
    with open(current_directory + '/local_config.json', 'r') as file:
        config = json.load(file)
    # 访问配置数据

    url = config["url"]+ "/select_user"

    logger.info(f"select_user url：{url}")

    # url = "http://127.0.0.1:5000/select_user"

    # url = "http://103.149.200.228:8081/select_user"
    response = requests.post(url, json=json.dumps(data))

    # 从回复中 中获取并解析 JSON 数据

    if json.loads(response.text) == "None":
        print("None")
    else:
        user_message_list = json.loads(response.text)
        print(user_message_list)
        return user_message_list

def add_assistant_start(user, device_id, assistant_message):

    # 获取原来userlist
    user_message_list = select_user(user, device_id)

    if user_message_list == None:
        user_message_list = []

    massage = []
    massage.append('assistant')
    massage.append(assistant_message)
    user_message_list.append(massage)

    data = {
        'device_id': device_id,
        'user': user,
        'user_message_list': user_message_list,
    }

    # 获取当前脚本的文件路径
    current_file_path = os.path.abspath(__file__)

    # 获取当前脚本所在的文件夹路径
    current_directory = os.path.dirname(current_file_path)

    # 读取 JSON 文件
    with open(current_directory + '/local_config.json', 'r') as file:
        config = json.load(file)
    # 访问配置数据

    url = config["url"] + "/add_user"

    logger.info(f"add_assistant_start url：{url}")

    response = requests.post(url, json=json.dumps(data))

    text = response.text

    logger.info(f"add_assistant_start 请求响应：{text}")

    print(response)



if __name__ == "__main__":


   add_assistant_start("王您","s1","好热呀")

   # add_user()

   # device_user_dict = get_device_user_dict("清")
   # print(device_user_dict)